/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.deployment.manager.editar;

import br.com.deployment.entidade.fermojupi.AtoCampo;
import br.com.deployment.entidade.fermojupi.AtoItem;
import br.com.deployment.entidade.fermojupi.AtoTipo;
import br.com.deployment.entidade.selo.TipoSelo;
import br.com.deployment.message.Msg;
import br.com.deployment.servico.AtoCampoServico;
import br.com.deployment.servico.AtoItemServico;
import br.com.deployment.servico.AtoTipoServico;
import br.com.deployment.servico.TipoSeloServico;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import org.primefaces.event.SelectEvent;
import org.primefaces.model.DualListModel;

/**
 *
 * @author elyjr
 */
@ManagedBean
@SessionScoped
public class ManagerEditarAtoItem implements Serializable{
    
    @EJB
    AtoItemServico atoItemServico;
    
    @EJB
    AtoTipoServico atoTipoServico;
    
    @EJB
    TipoSeloServico tipoSeloServico;
    
    
    @EJB
    AtoCampoServico atoCampoServico;
    
    private AtoItem atoItem;
    
    private TipoSelo tipoSelo;
    private DualListModel<AtoCampo> dualAtoCampo;
    private List<AtoCampo> atoCamposEsq;
    private List<AtoCampo> atoCampoDir;


    public void redirect(String string, AtoItem a) {
        try {
            FacesContext context = FacesContext.getCurrentInstance();
            tipoSelo = new TipoSelo();
            atoItem = a;
            init();
            context.getExternalContext().redirect(string);
        } catch (IOException ex) {
            Logger.getLogger(ManagerEditarAtoItem.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void init(){
        atoCampoDir = atoCampoServico.getCampos(atoItem);
        atoCamposEsq = atoCampoServico.atoCampos();
        atoCamposEsq.removeAll(atoCampoDir);
        dualAtoCampo = new DualListModel<AtoCampo>(atoCamposEsq, atoCampoDir);
    }
    
   public void handleSelect(SelectEvent event) {
        AtoTipo a = (AtoTipo) event.getObject();
        atoItem.setAtoTipo(atoTipoServico.find(a.getId()));
    }
   
    public void handleSelectTipoSelo(SelectEvent event) {
        
        tipoSelo = (TipoSelo) event.getObject();
        //atoItem.setTipoSelo(tipoSelo);
        atoItem.setTipoSelo(tipoSeloServico.find(tipoSelo.getId()));

    }
    
    
   
    public void alterar(){
         atoCampoDir = dualAtoCampo.getTarget(); 
         atoItem.setAtoCampos(atoCampoDir);
        
         atoItemServico.atualizar(atoItem);
         Msg.messagemInfo("Ato Atualizado com Sucesso !");
    } 
     
    
    public AtoItem getAtoItem() {
        return atoItem;
    }

    public void setAtoItem(AtoItem atoItem) {
        this.atoItem = atoItem;
    }

    public DualListModel<AtoCampo> getDualAtoCampo() {
        return dualAtoCampo;
    }

    public void setDualAtoCampo(DualListModel<AtoCampo> dualAtoCampo) {
        this.dualAtoCampo = dualAtoCampo;
    }
    
    
    
    
    
    
}
